<?php

namespace App\Console\Commands\OpenShop;

use App\Enums\OpenShop\InvestmentPromotion\OrganizationManageEnum;
use App\Models\OpenShop\Common\OpenShopOrderManagerModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserBaseInfoModel;
use App\Service\OpenShop\Feishu\OrganizationManageService;
use Illuminate\Console\Command;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class RepairPrepareUser extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openShop:repairPrepareUser';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '筹备提前介入 - 历史数据修复';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function handle(): int
    {

        OpenShopMainProcessModel::query()->whereBetween('nowStepStatus', [3,6])->get()->each(function (
            OpenShopMainProcessModel $openShopMainProcessModel
        ) {


            //以上2.3.步骤做insert，筹备负责人操作
            $orderNo       = $openShopMainProcessModel->orderNo;
            $mainProcessId = $openShopMainProcessModel->id;

            //获取订单基础信息
            $userBaseInfoObj = OpenShopUserBaseInfoModel::getDataByOrderNo($orderNo);
            //获取筹备uid
            $manager = OrganizationManageService::getOrganizationInfoByType(OrganizationManageEnum::TYPE_BRANCH_OFFICE,
                $userBaseInfoObj->chooseShopProvinceCode);


            if ($manager && $manager["preparationUid"] > 0) {
                //经理表写入筹备数据
                OpenShopOrderManagerModel::generateManagerOrUpdate(
                    OpenShopOrderManagerModel::MANAGER_TYPE_4,
                    $mainProcessId,
                    $manager["preparationUid"],
                    $manager["preparationName"]
                );

                $this->getOutput()->writeln(sprintf("订单号 ： %s", $orderNo));
            } else {
                $this->getOutput()->error(
                    vsprintf(
                        "订单号 ： %s 未匹配到筹备信息 省份code : %s",
                        [
                            $orderNo,
                            $userBaseInfoObj->chooseShopProvinceCode
                        ])
                );
            }

            //空间设计表写入
            $openShopSpaceDesignModel = OpenShopSpaceDesignModel::query()->where([
                "mainProcessId" => $mainProcessId,
                "orderNo"       => $orderNo
            ])->first();
            if (empty($openShopSpaceDesignModel)) {
                OpenShopSpaceDesignModel::query()->insert([
                    "mainProcessId"   => $mainProcessId,
                    "orderNo"         => $orderNo,
                    "createdAt"       => time(),
                    "preparationUid"  => $manager["preparationUid"] ?? 0,
                    "preparationName" => $manager["preparationName"] ?? ""
                ]);
            }
        });

        return parent::SUCCESS;
    }
}
